package  com.syqc.sensor.oil

import androidx.activity.viewModels
import androidx.core.view.isVisible
import autodispose2.autoDispose
import com.gyf.immersionbar.ktx.immersionBar
import com.syqc.comlib.base.BaseActivity
import com.syqc.comlib.utils.showToast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.comlib.utils.showToast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.utils.Keys
import com.syqc.entity.OilDay
import com.syqc.monitor.R
import com.syqc.monitor.chart.MPChartUtils
import com.syqc.monitor.databinding.ActivityRecentOilInfoBinding
import  com.syqc.sensor.AddLeakActivity
import com.syqc.utils.DataUtil 
import com.syqc.utils.ToolsUtil

class OilChartActivity :
    BaseActivity<ActivityRecentOilInfoBinding>(ActivityRecentOilInfoBinding::inflate) {
    private var mData: OilDay? = null
    private val mViewModel by viewModels<OilChartVM>()
    private lateinit var mUnit: String

    //是否优化
    private var isOpti = true

    //是否里程
    private var isKm = false
    override fun initView() {
        mData = DataUtil.stats
        plate = intent.getStringExtra(Keys.INTENT_PLATE) ?: ""
        val teamName = intent.getStringExtra(Keys.INTENT_TEAM_NAME) ?: ""
        val unitTime = getString(R.string.unit_num)
        val unitRice = getString(R.string.unit_rice)
        val mileUnit = getString(R.string.unit_mile)
        mData?.run {
            mUnit = unit
            binding.tvToolbarTitle.text = "$plate  $name  $day"
            immersionBar { titleBar(binding.rlToolbar) }

            binding.lrvOilTime.setRightText(day)
            binding.lrvOilTeam.setRightText(teamName)
            binding.lrvOilAdd.nextVisible(upTimes > 0)
            binding.lrvOilLeak.nextVisible(leakTimes > 0)
            binding.lrvOilAdd.setRightText("$upTimes$unitTime  $upTotal$unit")
            binding.lrvOilLeak.setRightText("$leakTimes$unitTime  $leakTotal$unit")
            binding.lrvOilStop.setRightText("$idlingTimes$unitTime $idlingTotal$unit")
            binding.lrvOilConsume.setRightText("$downTotal$unit")
            binding.lrvOilMile.setRightText(if (mile > 1000) "$mileKm $mileUnit" else "$mile $unitRice")
            binding.lrvOilRun.setRightText("$runTotal$unit")
            binding.lrvOilRun100.setRightText("$runBy100km$unit")
            binding.lrvOilTotal100.setRightText("$totalBy100km$unit")

            mViewModel.handleAddLeak(list)
            mViewModel.handleData(DataUtil.oilPints!!)
        }

        MPChartUtils.initTempLineChart(binding.oilLineChart, "#EAA878", "#00618C", "#5A6B01", mUnit)

        mViewModel.oilDetail().observe(this) {
            MPChartUtils.initXAxis(binding.oilLineChart, mViewModel.timeData)
            MPChartUtils.setLineDataSet2(
                binding.oilLineChart,
                mViewModel.details,
                it[0],
                it[1],
                it[2],
                it[3],
                getString(R.string.desc_com_oil),
                mUnit
            ).isOpt = isOpti

            MPChartUtils.changeLineData(binding.oilLineChart, isOpti)
        }
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
    }


    override fun setListener() {
        binding.ivToolbarStart.setOnClickListener {
            finish()
        }

        binding.tvToolbarAction.setOnClickListener {
            if (binding.llOilState.isVisible) {
                binding.llOilState.isVisible = false
                binding.tvToolbarAction.setText(R.string.desc_com_display)
            } else {
                binding.llOilState.isVisible = true
                binding.tvToolbarAction.setText(R.string.desc_com_hide)
            }
        }

        binding.tvOilOpti.setOnClickListener {
            isOpti = !isOpti
            binding.tvOilOpti.isSelected = isOpti
            MPChartUtils.changeLineData(binding.oilLineChart, isOpti, isKm)
        }

        binding.tvLineSpeed.setOnClickListener {
            isKm = false
            binding.tvLineSpeed.isChecked = true
            MPChartUtils.changeLineData(binding.oilLineChart, isOpti, isKm)
        }

        binding.tvLineMile.setOnClickListener {
            isKm = true
            binding.tvLineMile.isChecked = true
            MPChartUtils.changeLineData(binding.oilLineChart, isOpti, isKm)
        }
        binding.lrvOilAdd.setOnClickListener {
            if (ToolsUtil.isFastClick()) return@setOnClickListener
            mData?.let {
                if (it.upTimes > 0) {
                    if (mViewModel.addDetail.isNullOrEmpty()) {
                        showToast(R.string.desc_toast_no_data)
                        return@setOnClickListener
                    }
                    DataUtil.addLeak = mViewModel.addDetail
                    startActivityTo<AddLeakActivity>(this) {
                        putExtra(Keys.INTENT_TITLE, plate)
                        putExtra(Keys.INTENT_OTHER, mUnit)
                        putExtra(Keys.INTENT_TYPE, 0)
                    }
                }
            }
        }
        binding.lrvOilLeak.setOnClickListener {
            if (ToolsUtil.isFastClick()) return@setOnClickListener
            mData?.let {
                if (it.leakTimes > 0) {
                    if (mViewModel.leakDetail.isNullOrEmpty()) {
                        showToast(R.string.desc_toast_no_data)
                        return@setOnClickListener
                    }
                    DataUtil.addLeak = mViewModel.leakDetail
                    startActivityTo<AddLeakActivity>(this) {
                        putExtra(Keys.INTENT_TITLE, plate)
                        putExtra(Keys.INTENT_OTHER, mUnit)
                        putExtra(Keys.INTENT_TYPE, 1)
                    }
                }
            }
        }

    }

    private var plate: String = ""


    override fun onDestroy() {
        super.onDestroy()

        MPChartUtils.clearData()
    }

}